package com.jianz.campus_markets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jianz.campus_markets.domain.ResponseResult;
import com.jianz.campus_markets.domain.dto.ProductCommentDto;
import com.jianz.campus_markets.domain.dto.UserChatDto;
import com.jianz.campus_markets.domain.entity.Comment;
import com.jianz.campus_markets.domain.entity.Product;
import com.jianz.campus_markets.mapper.CommentMapper;
import com.jianz.campus_markets.mapper.ProductMapper;
import com.jianz.campus_markets.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jianz.campus_markets.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jianz
 * @since 2023-12-28
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {


    @Override
    public ResponseResult addProductComment(ProductCommentDto commentDto) {

        Comment comment = BeanCopyUtils.copyBean(commentDto,Comment.class);

        comment.setToCommentUserId(-1L);

        save(comment);
        return ResponseResult.okResult("商品评论成功");
    }

    @Override
    public ResponseResult addUserChat(UserChatDto userChatDto) {

        Comment comment = BeanCopyUtils.copyBean(userChatDto,Comment.class);

        comment.setProductId(-1);

        save(comment);

        return ResponseResult.okResult("发送消息成功");
    }

    @Override
    public ResponseResult getComment(int productId) {

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getProductId,productId);

        List<Comment> comments = getBaseMapper().selectList(queryWrapper);
        return ResponseResult.okResult(comments);
    }

    @Override
    public ResponseResult getChat(int userId) {

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getUserId,userId);

        List<Comment> comments = getBaseMapper().selectList(queryWrapper);
        return ResponseResult.okResult(comments);
    }

    @Override
    public ResponseResult getToChat(int toCommentUserId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getToCommentUserId,toCommentUserId);

        List<Comment> comments = getBaseMapper().selectList(queryWrapper);
        return ResponseResult.okResult(comments);
    }


}
